var sksh = {
    setBtnStatus: function () {
        var row = $('#grid_sksh').datagrid('getSelected');

        if (row && row.id && row.zt) {
            //已保存 ybc  已入账  yrz   已审核  ysh   已提交  ytj  已退回 yth  已调整 ytz
            if (row.zt == 'ybc') {//已保存
                $("#btn_sksh_mod").hide();//修改
                $("#btn_sksh_del").show();//删除
                $("#btn_sksh_check").show();//入账
                $("#btn_sksh_tj").hide();//提交
                $("#btn_sksh_confirm").hide();//审核   领导权限
                $("#btn_sksh_kxtz").hide();//调整
                $("#btn_sksh_kxhz").hide();// 已划转
//                $("#btn_sksh_plrz").show();//批量入账
//                $("#btn_sksh_th").hide();//退回提交  领导权限
//                $("#btn_sksh_qxsh").hide();//取消审核  领导权限
//                $("#btn_sksh_xgshsj").hide();//修改审核时间
            } else if (row.zt == 'yrz') {//已入账
                $("#btn_sksh_mod").hide();//修改
                $("#btn_sksh_del").hide();//删除
                $("#btn_sksh_tj").show();//提交
                $("#btn_sksh_check").hide();//入账
                $("#btn_sksh_confirm").hide();//审核   领导权限
                $("#btn_sksh_kxtz").hide();//调整
                $("#btn_sksh_kxhz").hide();// 已划转
//                $("#btn_sksh_plrz").show();//批量入账
//                $("#btn_sksh_th").hide();//退回提交  领导权限
//                $("#btn_sksh_qxsh").hide();//取消审核  领导权限
//                $("#btn_sksh_xgshsj").hide();//修改审核时间
            } else if (row.zt == 'ytj') {//已提交
                $("#btn_sksh_mod").hide();//修改
                $("#btn_sksh_del").hide();//删除
                $("#btn_sksh_tj").hide();//提交
                $("#btn_sksh_check").hide();//入账
                $("#btn_sksh_confirm").show();//审核   领导权限
                $("#btn_sksh_kxtz").hide();//调整
                $("#btn_sksh_kxhz").hide();// 已划转
//                $("#btn_sksh_plrz").hide();//批量入账
//                $("#btn_sksh_th").show();//退回提交  领导权限
//                $("#btn_sksh_qxsh").hide();//取消审核  领导权限
//                $("#btn_sksh_xgshsj").hide();//修改审核时间
            } else if (row.zt == 'ysh') {//已审核
                $("#btn_sksh_mod").hide();//修改
                $("#btn_sksh_del").hide();//删除
                $("#btn_sksh_tj").hide();//提交
                $("#btn_sksh_check").hide();//入账
                $("#btn_sksh_confirm").hide();//审核   领导权限
                $("#btn_sksh_kxtz").show();//调整
                $("#btn_sksh_kxhz").hide();// 已划转
//                $("#btn_sksh_plrz").hide();//批量入账
//                $("#btn_sksh_th").hide();//退回提交  领导权限
//                $("#btn_sksh_qxsh").show();//取消审核  领导权限
//                $("#btn_sksh_xgshsj").show();//修改审核时间
            } else if (row.zt == 'yth') {//已退回提交
                $("#btn_sksh_mod").hide();//修改
                $("#btn_sksh_del").show();//删除
                $("#btn_sksh_tj").show();//提交
                $("#btn_sksh_check").show();//入账
                $("#btn_sksh_confirm").hide();//审核   领导权限
                $("#btn_sksh_kxtz").hide();//调整
                $("#btn_sksh_kxhz").hide();// 已划转
//                $("#btn_sksh_plrz").show();//批量入账
//                $("#btn_sksh_th").hide();//退回提交  领导权限
//                $("#btn_sksh_qxsh").hide();//取消审核  领导权限
//                $("#btn_sksh_xgshsj").hide();//修改审核时间
            } else if (row.zt == "ytz" && row.ye > 0) {
                $("#btn_sksh_mod").hide();//修改
                $("#btn_sksh_del").hide();//删除
                $("#btn_sksh_tj").hide();//提交
                $("#btn_sksh_check").hide();//入账
                $("#btn_sksh_confirm").hide();//审核   领导权限
                $("#btn_sksh_kxtz").hide();//调整
                $("#btn_sksh_kxhz").show();// 划转
            } else {
                $("#btn_sksh_mod").hide();//修改
                $("#btn_sksh_del").hide();//删除
                $("#btn_sksh_tj").hide();//提交
                $("#btn_sksh_check").hide();//入账
                $("#btn_sksh_confirm").hide();//审核   领导权限
                $("#btn_sksh_kxtz").hide();//调整
                $("#btn_sksh_kxhz").hide();// 划转
//                $("#btn_sksh_plrz").hide();//批量入账
//                $("#btn_sksh_th").hide();//退回提交  领导权限
//                $("#btn_sksh_qxsh").hide();//取消审核  领导权限
//                $("#btn_sksh_xgshsj").hide();//修改审核时间
            }
        } else {
            $("#btn_sksh_mod").hide();//修改
            $("#btn_sksh_del").hide();//删除
            $("#btn_sksh_tj").hide();//提交
            $("#btn_sksh_check").hide();//入账
            $("#btn_sksh_confirm").hide();//审核   领导权限
            $("#btn_sksh_kxtz").hide();//调整
            $("#btn_sksh_kxhz").hide();// 划转
//            $("#btn_sksh_th").hide();//退回提交  领导权限

//            $("#btn_sksh_qxsh").hide();//取消审核  领导权限
//            $("#btn_sksh_xgshsj").hide();//修改审核时间
        }
    },
    alwayShowDetail: function (rowData) {
        $('#layout_sksh').layout('remove', 'center');
        $('#layout_sksh').layout('add', {
            region: 'center',
            split: true,
            href: $.webPath() + '/sfgl/sksh/sksh_detail?skshId=' + rowData.id
        });
    }
}
var clickRowIndex = null;
$(function () {
    //加载企业机构
    $("#sksh_gldw").gsjgqczcombobox();
    var sysDate = $("#sksh_sysDate").val();
    //款项用途
    $("#sksh_kxsy").sjzdcombobox("kxsy");
    //表格初始加载
    $('#grid_sksh').datagrid({
        url: $.webPath() + "/sfgl/sksh/skshList",
        toolbar: "#toolbar_sksh",
        fitColumns: true,
        singleSelect: true,
        pagination: true,
        pageNumber: 1,
        fit: true,
        rownumbers: true,
        striped: true,
        showFooter: true,
        columns: [
            [
                {field: 'bm', title: '单据号', width: 25, align: 'center'},
                /*{field: 'sfklx', title: '收款类型', width: 15,
                 formatter: function (value) {
                 if (value == '3')
                 return '销售收款';
                 else
                 return '供应退款';
                 }, align: 'center'
                 },*/
                {field: 'gldw_qc', title: '关联企业', width: 40, align: 'center'},
                {field: 'sfje', title: '收款金额(元)', width: 25,
                    formatter: function (value) {
                        var sl = formatJe2(value, ",");
                        return sl;
                    },
                    styler: function () {
                        return 'color:green;font-size:14px;';
                    }, align: 'right'
                },
                /*      {field: 'kxlx', title: '款项类型', width: 15,
                 formatter: function (value) {
                 if (value == '1')
                 return '预付款';
                 if (value == '2')
                 return '应付款';
                 if (value == '3')
                 return '预收款';
                 if (value == '4')
                 return '应收款';
                 }, align: 'center'
                 },*/
                {field: 'khfksj', title: '客户付款时间', width: 20,
                    formatter: function (value) {
                        var val = formatDate(sysDate, value, 5);
                        return val
                    }, align: 'center'},
                {field: 'pjlx_z', title: '票据类型', width: 15, align: 'center'},
                /* {field: 'pjhm', title: '票据号码', width: 20, align: 'center'},*/
                {field: 'fplx', title: '款项类型', width: 15,
                    formatter: function (value) {
                        if (value == '0')
                            return '非计划现货';
                        if (value == '1')
                            return '物流金融';

                    }, align: 'center'
                },
                {field: 'kxsy_z', title: '款项用途', width: 15, align: 'center'},
                {field: 'fkfs_z', title: '付款方式', width: 15, align: 'center'},
                {field: 'sbgs_jc', title: '申报公司', width: 30, align: 'center'},
                {field: 'sbr_xm', title: '申报人', width: 15, align: 'center'},
                {field: "ye", title: "余额(元)", width: 20, align: 'center'},
                {field: "shrq", title: "审核日期", width: 20, align: 'center'} ,
                {field: "zt", title: "状态", width: 20,
                    formatter: function (value) {
                        if (value == 'ybc') {
                            return '已保存';
                        } else if (value == 'yrz') {
                            return '已入账';
                        } else if (value == 'ysh') {
                            return '已审核';
                        } else if (value == 'ytj') {
                            return '已提交';
                        } else if (value == 'ysd') {
                            return '已锁定';
                        } else if (value == 'yjs') {
                            return '已解锁';
                        } else if (value == 'yth') {
                            return '已退回';
                        } else if (value == 'ytz') {
                            return '已调整';
                        }
                    }, align: 'center'},
                {field: "sfhz", title: "是否划转", width: 10, align: 'center',
                    formatter: function (value) {
                        if (value == '0') {
                            return "未划转"
                        } else {
                            return "已划转"
                        }
                    }
                }
            ]
        ],
        onLoadSuccess: function () {
            var rows = $(this).datagrid("getRows");
            if (rows.length > 0) {
                $(this).datagrid("selectRow", 0);
                clickRowIndex = 0;
            }
            sksh.setBtnStatus();
            sksh.alwayShowDetail(rows[0]);
            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
        },
        onClickRow: function (rowIndex, rowData) {
            sksh.setBtnStatus();
            if (clickRowIndex == rowIndex)
                return;
            clickRowIndex = rowIndex;
        },
        onSelect: function (rowIndex, rowData) {
            sksh.setBtnStatus();
            if (clickRowIndex == rowIndex)
                return;
            clickRowIndex = rowIndex;
            sksh.alwayShowDetail(rowData);
        }
    });
    //**********按钮触发事件**********
    //新增按钮
    $("#btn_sksh_add").click(function () {
        sksh_add('新增收款单', 'add_sksh');
    });

    //充值按钮
    $("#btn_sksh_cz").click(function () {
        sksh_cz('充值收款单', 'cz_sksh');
    });

    //修改按钮
    $("#btn_sksh_mod").click(function () {
        sksh_modify('修改收款单', 'update_sksh');
    });

    //删除按钮
    $("#btn_sksh_del").click(function () {
        sksh_delete();
    });

    //审核按钮
    $("#btn_sksh_confirm").click(function () {
        sksh_confirm();
    });

    //修改审核时间
    $("#btn_sksh_xgshsj").click(function () {
        sksh_xgshsj('修改审核时间', 'sksh_xgshsj');
    });

    //款项调整
    $("#btn_sksh_kxtz").click(function () {
        sksh_kxtz('款项调整');
    });

    //款项划转
    $("#btn_sksh_kxhz").click(function () {
        sksh_kxhz('款项划转');
    });

    //提交按钮
    $("#btn_sksh_tj").click(function () {
        sksh_tj();
    });
    //退回提交
    $("#btn_sksh_th").click(function () {
        sksh_th();
    });
    //取消退回提交
    $("#btn_sksh_qxsh").click(function () {
        sksh_qxsh();
    });

    //入账按钮
    $("#btn_sksh_check").click(function () {
        var row = $('#grid_sksh').datagrid('getSelected');
        /* if (row.pjlx_z == '银票' || row.pjlx_z == '商票') {
         sksh_check_pj('审核票据', 'sksh_sh_pj'); //如果类型是银票商票就需要弹出不一样的
         }
         if (row.pjlx_z == '信用证') {
         sksh_check_xyz('审核信用证', 'sksh_sh_xyz'); //如果类型是信用证
         } else if (row.pjlx_z != '银票' && row.pjlx_z != '商票' && row.pjlx_z != '信用证') {
         sksh_check('审核应收款', 'sksh_sh');
         }*/
        if (row.pjlx_z == '信用证') {
            sksh_check_xyz('审核信用证', 'sksh_sh_xyz'); //如果类型是信用证
        } else {
            sksh_check('审核应收款', 'sksh_sh');
        }
    });
//    $("#btn_sksh_charge").click(function () {
//        var row = $('#grid_sksh').datagrid('getSelected');
//        $.messager.confirm('提示', '是否确定冲销？', function (r) {
//            if (r) {
//                $.post($.webPath() + '/sfgl/fksh/charge_fksh', {'fkshId': row.id}, function () {
//                    $('#grid_sksh').datagrid('reload');
//                });
//            }
//        })
//    })
    //查询按钮
    $("#btn_sksh_search").click(function () {
        sksh_search();
    });
    //双击事件
    $("#grid_sksh").datagrid({
        onDblClickCell: function (index, field) {
            $('#grid_sksh').datagrid('selectRow', index);
            var row = $('#grid_sksh').datagrid('getSelected');
            if (field == "bm" && row.zt != 3) {
                sksh_modify('修改收款单', 'update_sksh');
            }
        }
    });
    //加载查询条件中的申报公司下拉框
    $("#sksh_sbgs").combotree({
        loadFilter: function (rows) {
            return convert(rows);
        }
    });
});

//审核应收款
function sksh_check(title, action) {
    var row = $('#grid_sksh').datagrid('getSelected');
    var href = $.webPath() + "/sfgl/sksh/" + action + "?skshId=" + row.id;
    var actionUrl = $.webPath() + "/sfgl/sksh/check_sksh";
    sksh_sh_showDialog(title, href, actionUrl);
}

//审核应收款（票据）
function sksh_check_pj(title, action) {
    var row = $('#grid_sksh').datagrid('getSelected');
    var href = $.webPath() + "/sfgl/sksh/" + action + "?skshId=" + row.id;
    var actionUrl = $.webPath() + "/sfgl/sksh/check_sksh_pj";
    sksh_sh_pj_showDialog(title, href, actionUrl);
}

//审核应收款（信用证）
function sksh_check_xyz(title, action) {
    var row = $('#grid_sksh').datagrid('getSelected');
    var href = $.webPath() + "/sfgl/sksh/" + action + "?skshId=" + row.id;
    var actionUrl = $.webPath() + "/sfgl/sksh/check_sksh_xyz";
    sksh_sh_xyz_showDialog(title, href, actionUrl);
}


//新增收款管理
function sksh_add(title, action) {
    var href = $.webPath() + "/sfgl/sksh/sksh_content";
    var actionUrl = $.webPath() + "/sfgl/sksh/add_sksh";
    sksh_showDialog(title, href, actionUrl);
}

//新增收款管理
function sksh_cz(title, action) {
    var href = $.webPath() + "/sfgl/sksh/sksh_cz_content";
    var actionUrl = $.webPath() + "/sfgl/sksh/cz_sksh";
    sksh_cz_showDialog(title, href, actionUrl);
}

//修改收款管理
function sksh_modify(title, action) {
    var record = $("#grid_sksh").datagrid("getSelected");
    if (record == null) {
        $.messager.alert("提示", "请选择要更新的记录!", "warning");
        return;
    }
    if (record.kxyt_z == '充值') {
        $.messager.alert("提示", "充值的收付款不可修改!", "warning");
        return;
    }
    if (record.zt == 'ysh') return;
    var href = $.webPath() + "/sfgl/sksh/sksh_content?skshId=" + record.id;
    var actionUrl = $.webPath() + "/sfgl/sksh/" + action;
    sksh_showDialog(title, href, actionUrl);
}

function sksh_xgshsj(title, action) {
    var record = $('#grid_sksh').datagrid('getSelected');
    if (record == null) {
        $.messager.alert("提示", "请选择要修改的销售时间!", "warning");
        return;
    }
    if (!record.zt == 'ysh') {
        $.messager.alert("提示", "请选择已经审核过的付款!", "warning");
        return;
    }
    var href = $.webPath() + "/sfgl/sksh/to_sksh_xgshsj?skshId=" + record.id;
    var actionUrl = $.webPath() + "/sfgl/sksh/sksh_xgshsj";
    sksh_xgshsjDialog(title, href, actionUrl);
}

// 款项调整
function sksh_kxtz(title) {
    var row = $('#grid_sksh').datagrid('getSelected');
    if (row == null) {
        $.messager.alert("提示", "请选择要调整的收款!", "warning");
        return;
    }
    var href = $.webPath() + "/sfgl/sksh/sksh_kxtz?skshId=" + row.id;
    var actionUrl = $.webPath() + "/sfgl/sksh/sksh_kxtz_save";
    sksh_kxtz_showDialog(title, href, actionUrl);
}

// 款项划转
function sksh_kxhz(title) {
    var row = $('#grid_sksh').datagrid('getSelected');
    if (row == null) {
        $.messager.alert("提示", "请选择要划转的收款!", "warning");
        return;
    }
    var href = $.webPath() + "/sfgl/sksh/sksh_kxhz?skshId=" + row.id;
    var actionUrl = $.webPath() + "/sfgl/sksh/sksh_kxhz_save";
    sksh_kxhz_showDialog(title, href, actionUrl);
}

//弹出新增或修改的窗口
function sksh_showDialog(title, href, actionUrl) {
    var win = $.window({
        title: title,
        width: 620,
        height: 360,
        cache: false,
        url: href,
        modal: true,
        buttons: [
            {
                text: '保存',
                handler: function () {
                    $(win).mask();
                    $('#form_sksh').form('submit', {
                        url: actionUrl,
                        onSubmit: function () {
                            $("[name='sksh.zt']").val("ybc");
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                $(win).mask('hide');
                                return false;
                            }
                            return isValid;
                        },
                        success: function (data) {
//                            $(win).window('close');
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#grid_sksh').datagrid('reload');
                                $('#layout_sksh').layout('panel', 'center').panel('refresh');
                                $.messager.show({
                                    title: '提示',
                                    msg: json.message,
                                    timeout: 3000,
                                    showType: 'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ]
    });
}

//弹出新增或修改的窗口
function sksh_cz_showDialog(title, href, actionUrl) {
    var win = $.window({
        title: title,
        width: 620,
        height: 410,
        cache: false,
        url: href,
        modal: true,
        buttons: [
            {
                text: '保存',
                handler: function () {
                    $(win).mask();
                    $('#form_sksh').form('submit', {
                        url: actionUrl,
                        onSubmit: function () {
                            $("[name='sksh.zt']").val("ybc");

                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                $(win).mask('hide');
                                return false;
                            }
                            // 如果承兑判断
                            if ($("#sksh_fkfs").combobox('getText') == '承兑') {
                                if (!$("#sksh_startDate").datebox('getValue')) {
                                    $.messager.alert('提示', "开票日期不能为空!", 'warning');
                                    $(win).mask('hide');
                                    return false;
                                } else if (!$("#sksh_endDate").datebox('getValue')) {
                                    $.messager.alert('提示', "到期日期不能为空!", 'warning');
                                    $(win).mask('hide');
                                    return false;
                                } else if (!$("#sksh_pjhm").val()) {
                                    $.messager.alert('提示', "票据号码不能为空！", 'warning');
                                    $(win).mask('hide');
                                    return false;
                                }
                            }
                            return isValid;
                        },
                        success: function (data) {
//                            $(win).window('close');
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#grid_sksh').datagrid('reload');
                                $('#layout_sksh').layout('panel', 'center').panel('refresh');
                                $.messager.show({
                                    title: '提示',
                                    msg: json.message,
                                    timeout: 3000,
                                    showType: 'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ]
    });
}

//弹出审核应收款窗口（票据）
function sksh_sh_pj_showDialog(title, href, actionUrl) {
    var win = $.window({
        title: title,
        width: 760,
        height: 430,
        cache: false,
        url: href,
        modal: true,
        buttons: [
            {
                text: '确认',
                handler: function () {
                    $(win).mask();
                    $('#form_sksh_sh').form('submit', {
                        url: actionUrl,
                        onSubmit: function (param) {
                            var r = $('#sksh_pj_grid').edatagrid("getSelected");
                            var index = $('#sksh_pj_grid').edatagrid("getRowIndex", r);
                            $('#sksh_pj_grid').edatagrid("endEdit", index);
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                $(win).mask('hide');
                                return false;
                            }
                            var data = $('#sksh_pj_grid').edatagrid("getData");
                            if (!data.rows.length) {
                                $(win).mask('hide');
                                $.messager.alert("提示", "请填写!", "waring");
                                return false;
                            }
                            var arrayPrefix = 'sksh.pjList';
                            //物资计划明细
                            var paramString = "{";
                            //物资计划明细
                            for (var i = 0; i < data.rows.length; i++) {
                                var objectString = $.toJSONStringPrefix(data.rows[i], arrayPrefix + "[" + i + "].");
                                paramString = paramString + objectString.substring(1, objectString.length - 1) + ",";
                            }
                            if (data.rows && data.rows.length > 0)
                                paramString = paramString.substring(0, paramString.length - 1);
                            paramString += "}";
                            $.extend(param, $.parseJSON(paramString));
                            return isValid;
                        },
                        success: function (data) {
                            $(win).mask('hide');
                            $('#layout_sksh').layout('panel', 'center').panel('refresh');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#grid_sksh').datagrid('reload');
                                $.messager.show({
                                    title: '提示',
                                    msg: json.message,
                                    timeout: 3000,
                                    showType: 'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ]
    });
}

//弹出审核应收款窗口（信用证）
function sksh_sh_xyz_showDialog(title, href, actionUrl) {
    var win = $.window({
        title: title,
        width: 760,
        height: 430,
        cache: false,
        url: href,
        modal: true,
        buttons: [
            {
                text: '确认',
                handler: function () {
                    $(win).mask();
                    $('#form_sksh_sh').form('submit', {
                        url: actionUrl,
                        onSubmit: function (param) {
                            var r = $('#sksh_xyz_grid').edatagrid("getSelected");
                            var index = $('#sksh_xyz_grid').edatagrid("getRowIndex", r);
                            $('#sksh_xyz_grid').edatagrid("endEdit", index);
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                $(win).mask('hide');
                                return false;
                            }
                            var data = $('#sksh_xyz_grid').edatagrid("getData");
                            if (!data.rows.length) {
                                $(win).mask('hide');
                                $.messager.alert("提示", "请填写!", "waring");
                                return false;
                            }
                            var arrayPrefix = 'sksh.xyzList';
                            //物资计划明细
                            var paramString = "{";
                            //物资计划明细
                            for (var i = 0; i < data.rows.length; i++) {
                                var objectString = $.toJSONStringPrefix(data.rows[i], arrayPrefix + "[" + i + "].");
                                paramString = paramString + objectString.substring(1, objectString.length - 1) + ",";
                            }
                            if (data.rows && data.rows.length > 0)
                                paramString = paramString.substring(0, paramString.length - 1);
                            paramString += "}";
                            $.extend(param, $.parseJSON(paramString));
                            return isValid;
                        },
                        success: function (data) {
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#grid_sksh').datagrid('reload');
                                $.messager.show({
                                    title: '提示',
                                    msg: json.message,
                                    timeout: 3000,
                                    showType: 'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ]
    });
}

// 弹出审核应收款窗口
function sksh_sh_showDialog(title, href, actionUrl) {
    var win = $.window({
        title: title,
        width: 620,
        height: 360,
        cache: false,
        url: href,
        modal: true,
        buttons: [
            {
                text: '确认',
                handler: function () {
                    $(win).mask();
                    $('#form_sksh_sh').form('submit', {
                        url: actionUrl,
                        onSubmit: function (param) {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                $(win).mask('hide');
                                return false;
                            }
                            return isValid;
                        },
                        success: function (data) {
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#grid_sksh').datagrid('reload');
                                $.messager.show({
                                    title: '提示',
                                    msg: json.message,
                                    timeout: 3000,
                                    showType: 'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ]
    });
}

// 弹出应收款调整窗口
function sksh_kxtz_showDialog(title, href, actionUrl) {
    var win = $.window({
        title: title,
        width: 620,
        height: 360,
        cache: false,
        url: href,
        modal: true,
        buttons: [
            {
                text: '确认',
                handler: function () {
                    $(win).mask();
                    $('#form_sksh_kxtz').form('submit', {
                        url: actionUrl,
                        onSubmit: function (param) {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                $(win).mask('hide');
                                return false;
                            }
                            // 如果承兑判断
                            if ($("#sksh_fkfs_tz").val() == '承兑') {
                                if (!$("#sksh_mxtx").val()) {
                                    $.messager.alert('提示', "免息天数不能为空！", 'warning');
                                    $(win).mask('hide');
                                    return false;
                                } else if (!$("#sksh_txl").val()) {
                                    $.messager.alert('提示', "贴息率不能为空！", 'warning');
                                    $(win).mask('hide');
                                    return false;
                                }
                            }
                            return isValid;
                        },
                        success: function (data) {
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#grid_sksh').datagrid('reload');
                                $.messager.show({
                                    title: '提示',
                                    msg: json.message,
                                    timeout: 3000,
                                    showType: 'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ]
    });
}

// 弹出应收款划转窗口
function sksh_kxhz_showDialog(title, href, actionUrl) {
    var win = $.window({
        title: title,
        width: 620,
        height: 360,
        cache: false,
        url: href,
        modal: true,
        buttons: [
            {
                text: '确认',
                handler: function () {
                    $(win).mask();
                    $('#form_sksh_kxhz').form('submit', {
                        url: actionUrl,
                        onSubmit: function (param) {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                $(win).mask('hide');
                                return false;
                            }
                            return isValid;
                        },
                        success: function (data) {
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#grid_sksh').datagrid('reload');
                                $.messager.show({
                                    title: '提示',
                                    msg: json.message,
                                    timeout: 3000,
                                    showType: 'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ]
    });
}

//删除收款管理
function sksh_delete() {
    var row = $('#grid_sksh').datagrid('getSelected');
    if (row && row.zt != "ytj" && row.zt != "ysh") {
        $.messager.confirm('提示', '是否删除该收款单？', function (r) {
            if (r) {
                var index = $('#grid_sksh').datagrid('getRowIndex', row);
                $.ajax({
                    url: $.webPath() + "/sfgl/sksh/delete_sksh",
                    dataType: 'json',
                    type: 'POST',
                    data: {skshId: row.id},
                    beforeSend: function () {
                        $("#layout_sksh").mask();
                    },
                    success: function (data) {
                        $("#layout_sksh").mask("hide");
                        //todo
                        if (data.statusCode == 200) {
                            $('#grid_sksh').datagrid('deleteRow', index);
                            $(this).datagrid('selectRow', 0);
                            $.messager.show({
                                title: '提示',
                                msg: data.message,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        } else {
                            $.messager.alert("提示", data.message, "error");
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要删除的收款单！", "warning");
    }
}

function sksh_xgshsjDialog(title, href, actionUrl) {
    var win = $.window({
        title: title,
        width: 420,
        height: 400,
        cache: false,
        url: href,
        modal: true,
        buttons: [
            {
                text: '保存',
                handler: function () {
                    $(win).mask();
                    $('#form_sksh_xgshsj').form('submit', {
                        url: actionUrl,
                        onSubmit: function () {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                $(win).mask('hide');
                                return false;
                            }
                            return isValid;
                        },
                        success: function (data) {
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#grid_sksh').datagrid('reload');
                                $('#sksh_layout').layout('panel', 'center').panel('refresh');
                                $.messager.show({
                                    title: '提示',
                                    msg: json.message,
                                    timeout: 3000,
                                    showType: 'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ]
    });
}

//审核收款管理
function sksh_confirm() {
    var row = $('#grid_sksh').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否确认该收款单？', function (r) {
            if (r) {
                $.ajax({
                    url: $.webPath() + "/sfgl/sksh/change_status",
                    dataType: 'json',
                    type: 'POST',
                    data: {skshId: row.id, zt: 'ysh'},
                    beforeSend: function () {
                        $("#layout_sksh").mask();
                    },
                    success: function (data) {
                        $("#layout_sksh").mask("hide");
                        if (data.statusCode == 200) {
                            $('#grid_sksh').datagrid('reload');
                            $.messager.show({
                                title: '提示',
                                msg: data.message,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        } else {
                            $.messager.alert("提示", data.message, "error");
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要审核的收款单！", "warning");
    }
}

//查询收款管理
function sksh_search() {
    var sbgs = $('#sksh_sbgs').combotree('getText') == null ? null : $('#sksh_sbgs').combotree('getText');
    var sbgsId = $('#sksh_sbgs').combotree('getValue') == null ? null : $('#sksh_sbgs').combotree('getValue');
    var zt = $('#sksh_zt').combobox('getValue') == null ? null : $('#sksh_zt').combobox('getValue');
    var kxsy = $('#sksh_kxsy').combobox('getValue') == null ? null : $('#sksh_kxsy').combobox('getValue');
    var skshStart = $('#sksh_start').datebox('getValue');
    var skshEnd = $('#sksh_end').datebox('getValue');
    $('#grid_sksh').datagrid('load',
        {
            bm: $('#sksh_bm').val(),
            sbgs: sbgs,
            sbgsId: sbgsId,
            gldwMc: $('#sksh_gldw').attr("_text") == null ? null : $('#sksh_gldw').attr("_text"),
            zt: zt,
            skr: $('#sksh_zdr').val(),
            skshStart: skshStart,
            skshEnd: skshEnd,
            kxsy: kxsy
        }
    );
}
//提交收款管理
function sksh_tj() {
    var row = $('#grid_sksh').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否提交收款单？', function (r) {
            if (r) {
                $.ajax({
                    url: $.webPath() + "/sfgl/sksh/sksh_tj",
                    dataType: 'text',
                    type: 'POST',
                    data: {skshId: row.id, zt: 'ytj'},
                    beforeSend: function () {
                        $("#sksh_layout").mask();
                    },
                    success: function (data) {
                        $("#sksh_layout").mask("hide");
                        if (data == '收款单已提交!') {
                            $('#grid_sksh').datagrid('reload');
                            $.messager.show({
                                title: '提示',
                                msg: data,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        } else {
                            $.messager.alert("提示", data, "error");
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要提交收款单！", "warning");
    }
}
//退回收款管理
function sksh_th() {
    var row = $('#grid_sksh').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否退回收款单？', function (r) {
            if (r) {
                $.ajax({
                    url: $.webPath() + "/sfgl/sksh/sksh_th",
                    dataType: 'text',
                    type: 'POST',
                    data: {skshId: row.id, zt: 'yth'},
                    beforeSend: function () {
                        $("#sksh_layout").mask();
                    },
                    success: function (data) {
                        $("#sksh_layout").mask("hide");
                        if (data == '收款单已退回!') {
                            $('#grid_sksh').datagrid('reload');
                            $.messager.show({
                                title: '提示',
                                msg: data,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        } else {
                            $.messager.alert("提示", data, "error");
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要退回收款单！", "warning");
    }
}

//批量入账
$("#btn_sksh_plrz").click(function () {
    var row = $('#grid_sksh').datagrid('getSelected');
    var win = $.window({
        title: "批量入账导入",
        width: 450,
        height: 240,
        cache: false,
        url: $.webPath() + "/sfgl/sksh/sksh_sh_pj_upload?skshId=" + row.id,
        modal: true,
        buttons: [
            {
                text: '确认',
                handler: function () {
                    $('#sksh_pj_uploadSubject').form('submit', {
                        onSubmit: function () {
                            $(win).mask();
                        },
                        success: function (data) {
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#grid_sksh').datagrid('reload');
                                $.messager.show({
                                    title: '提示',
                                    msg: json.message,
                                    timeout: 3000,
                                    showType: 'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ]
    });
});

//取消审核管理
function sksh_qxsh() {
    var row = $('#grid_sksh').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否取消审核收款单？', function (r) {
            if (r) {
                $.ajax({
                    url: $.webPath() + "/sfgl/sksh/sksh_qxsh",
                    dataType: 'text',
                    type: 'POST',
                    data: {skshId: row.id, zt: 'yth'},
                    beforeSend: function () {
                        $("#sksh_layout").mask();
                    },
                    success: function (data) {
                        $("#sksh_layout").mask("hide");
                        if (data == '收款单已取消审核!') {
                            $('#grid_sksh').datagrid('reload');
                            $.messager.show({
                                title: '提示',
                                msg: data,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        } else {
                            $.messager.alert("提示", data, "error");
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要退回收款单！", "warning");
    }
}